﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
using Quartz;
using Quartz.Impl;
using Common.Logging;
using EasyQuartz.Client.Jobs;
using System.Reflection;

namespace EasyQuartz.Client.Console
{
    public class Program
    {
		static NameValueCollection properties = GetProperties();
		
        static void Main(string[] args)
        {
			ILog log = LogManager.GetLogger(typeof(Program));
			log.Info("Try connect...");

			ISchedulerFactory sf = new StdSchedulerFactory(properties);
            IScheduler sched = sf.GetScheduler();

			// define the job and ask it to run	
			JobDetail job = new JobDetail("remotelyAddedJob", "default", typeof(HelloWorld));
			JobDataMap map = new JobDataMap();
			map.Put("msg", "Your remotely added job has executed!");
			job.JobDataMap = map;
			CronTrigger trigger = new CronTrigger("remotelyAddedTrigger", "default", "remotelyAddedJob", "default", DateTime.UtcNow, null, "0 30 10-13 ? * WED,FRI");

			// schedule the job
			sched.ScheduleJob(job, trigger);

			log.Info("Remote job scheduled.");
        }

		/// <summary>
		/// Get the properties to quartz config.
		/// </summary>
		/// <returns></returns>
		private static NameValueCollection GetProperties()
		{
			Configuration configuration = ConfigurationManager.OpenExeConfiguration(new Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath);
			NameValueCollection properties = new NameValueCollection();
           
            foreach (KeyValueConfigurationElement item in configuration.AppSettings.Settings)
				properties[item.Key] = item.Value;

			return properties;
		}
    }
}
